package com.taoyibao.hongcanting.ui.home.activity;

import android.content.Context;
import android.content.Intent;

import com.taoyibao.hongcanting.base.activity.BaseActivityPresenter;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.event.EventCartClearAll;
import com.taoyibao.hongcanting.event.EventFinishActivity;
import com.taoyibao.hongcanting.event.EventSubmitSuccess;
import com.taoyibao.hongcanting.event.EventUpdataCart;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.CartSimpleInfoModel;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.model.DishModel;
import com.taoyibao.hongcanting.model.FoodTypeModel;
import com.taoyibao.hongcanting.ui.home.delegate.MenuDelegate;
import com.taoyibao.hongcanting.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * @author TangAnna
 * @description: 点单页面
 * @date :${DATA} 16:40
 */
public class MenuActivity extends BaseActivityPresenter<MenuDelegate> {

    @Override
    protected Class<MenuDelegate> getDelegateClass() {
        return MenuDelegate.class;
    }

    /**
     * @param context
     * @param chooseDish 首页点击的推荐菜
     */
    public static void open(Context context, DishModel chooseDish) {
        Intent intent = new Intent(context, MenuActivity.class);
        intent.putExtra(CodeConstant.CURRENT_DISH, chooseDish);
        context.startActivity(intent);
    }

    /**
     * @param context
     */
    public static void open(Context context) {
        Intent intent = new Intent(context, MenuActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        EventBus.getDefault().register(this);
        getRecommend();
        getFoodLists();
        getShoppingCartData();
    }

    /**
     * 获取推荐菜品
     */
    public void getRecommend() {
        JsonApiWrapper.getApiService().recommend(SpUtils.getToken(), SpUtils.getMemberId())
                .compose(new DefaultTransformer<CompanyBaseModel<List<DishModel>>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<List<DishModel>>>(this, false) {
                    @Override
                    public void onNext(CompanyBaseModel<List<DishModel>> recommend) {
                        if (recommend.code == NetCode.HTTP_200) {
                            viewDelegate.setRecommendData(recommend.data);
                        } else {
                            super.onNext(recommend);
                        }
                    }
                });
    }

    /**
     * 网络获取点餐页面的分类数据
     */
    public void getFoodLists() {
        JsonApiWrapper.getApiService().foodLists(SpUtils.getToken(), SpUtils.getMemberId())
                .compose(new DefaultTransformer<CompanyBaseModel<List<FoodTypeModel>>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<List<FoodTypeModel>>>(this, true) {
                    @Override
                    public void onNext(CompanyBaseModel<List<FoodTypeModel>> foodTypeModel) {
                        if (foodTypeModel.code == NetCode.HTTP_200) {
                            if (foodTypeModel.data != null && foodTypeModel.data.size() > 0) {
                                foodTypeModel.data.get(0).isChecked = true;
                                viewDelegate.setTypeData(foodTypeModel.data);
                            }
                            viewDelegate.setDataView();
                        } else {
                            super.onNext(foodTypeModel);
                            //为了避免此页面变形  如果是300跳转登录页面之后关闭此页面
                            if (foodTypeModel.code == NetCode.HTTP_300) {
                                finish();
                            }
                        }
                    }
                });
    }


    /**
     * 操作菜品数量变化时调用
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void shoppingEvent(EventUpdataCart eventUpdataCart) {
        LogUtils.dNormal("TAG", "菜单页面收到的菜品" + eventUpdataCart.toString());
        getShoppingCartData();
        viewDelegate.onRefrush(eventUpdataCart);
    }

    /**
     * 订单提交成功关闭此页面
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void shoppingEvent(EventSubmitSuccess eventUpdataCart) {
        if (eventUpdataCart.isSuccess) {
            finish();
        }
    }


    /**
     * 查询购物车简单数据
     */
    public void getShoppingCartData() {
        JsonApiWrapper.getApiService().cartSimpleInfo(SpUtils.getToken(), SpUtils.getMemberId())
                .compose(new DefaultTransformer<CompanyBaseModel<List<CartSimpleInfoModel>>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<List<CartSimpleInfoModel>>>(this, false) {
                    @Override
                    public void onNext(CompanyBaseModel<List<CartSimpleInfoModel>> cartSimpleInfoModel) {
                        viewDelegate.setBottomUI(cartSimpleInfoModel.data);
                    }
                });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        SpUtils.putOtherSeatName("");
        SpUtils.putOtherSeatId("");
    }

    /**
     * 支付成功关闭页面
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventPayFinish(EventFinishActivity event) {
        if (event.paySuccess) {
            finish();
        }
    }

    /**
     * 清空购物车
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventCartClear(EventCartClearAll event) {
        if (event.isClearAll) {
            getRecommend();
            getFoodLists();
            getShoppingCartData();
        }
    }


}
